package com.soap.client;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.soap.constants.SoapConstants;
import com.soap.util.FileXmlUtil;
import com.soap.util.MD5Util;
import com.soap.util.AESUtil;

/**
 * WebService 发起请求
 * 
 * @author Roger
 */
public class SoapRequest {

	public static void main(String[] args) throws Exception {

		Date currentDate = new Date();
		String reqDate = new SimpleDateFormat("yyyyMMdd").format(currentDate);
		System.out.println(reqDate);
		String reqTime = new SimpleDateFormat("HHmmss").format(currentDate);
		System.out.println(reqTime);

		String sign = MD5Util.md5Encode(reqDate + reqTime
				+ SoapConstants.REQEUST_SIGN);
		System.out.println(sign);

		StringBuffer soapMessage = new StringBuffer();
		soapMessage.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		soapMessage.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
		soapMessage.append("  <soap:Body>");
		soapMessage.append("    <GetVipInfo xmlns=\"http://www.tech-trans.com.cn/\">");
		soapMessage.append("      <request>");
		soapMessage.append("        <Header>");
		soapMessage.append("          <REQDATE>" + reqDate + "</REQDATE>");
		soapMessage.append("          <REQTIME>" + reqTime + "</REQTIME>");
		soapMessage.append("          <SIGN>" + sign + "</SIGN>");
		soapMessage.append("          <USER>TEST</USER>");
		soapMessage.append("        </Header>");
		soapMessage.append("        <Data>");
		soapMessage.append("          <vipcode>"+AESUtil.encrypt("122333","201511061724")+"</vipcode>");
		soapMessage.append("        </Data>");
		soapMessage.append("      </request>");
		soapMessage.append("    </GetVipInfo>");
		soapMessage.append("  </soap:Body>");
		soapMessage.append("</soap:Envelope>");
		
		String soapAction = "http://www.tech-trans.com.cn/GetBasicInfo";

//		String response = soapRequest(soapMessage.toString(), soapAction);
		String response = soapRequest(FileXmlUtil.xmlToString(), soapAction);
		System.out.println(response);
	}

	/**
	 * Web Service 请求
	 * 
	 * @param soapMessage
	 *            请求参数
	 * @param soapAction
	 *            请求soapAction
	 * @return 响应XML
	 */
	public static String soapRequest(String soapMessage, String soapAction) {
		// 请求响应报文
		StringBuilder response = new StringBuilder();

		HttpURLConnection conn = null;
		try {
			conn = getConnection();
			conn.setRequestProperty(SoapConstants.CONTENT_LENGTH,
					String.valueOf(soapMessage.length()));
			conn.setRequestProperty(SoapConstants.SOAPACTION, soapAction);
			
			// request输出流
			OutputStream output = conn.getOutputStream();
			if (null != soapMessage) {
				byte[] b = soapMessage.getBytes("utf-8");
				// 发送soap请求报文
				output.write(b, 0, b.length);
			}
			output.flush();
			output.close();

			// 获取soap响应报文
			InputStream input = conn.getInputStream();
			
			getResponseMessage(input, response);

			input.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			conn.disconnect();
		}
		return response.toString();
	}

	/**
	 * 获取响应数据，进行转码
	 * 
	 * @param input
	 *            InputStream
	 * @param response
	 *            响应数据
	 * @throws Exception
	 */
	public static void getResponseMessage(InputStream input,
			StringBuilder response) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(input,
				"UTF-8"));
		int c = -1;
		// sb为返回的soap响应报文字符串
		while (-1 != (c = br.read())) {
			response.append((char) c);
		}
	}

	/**
	 * 获取HttpURLConnection
	 * 
	 * @return HttpURLConnection
	 * @throws Exception
	 */
	public static HttpURLConnection getConnection() throws Exception {
		// 设置soap请求报文的相关属性
		URL url = new URL(SoapConstants.REQEUST_WSDL_URL);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoInput(true);
		conn.setDoOutput(true);
		conn.setUseCaches(false);
		conn.setDefaultUseCaches(false);
		conn.setRequestProperty(SoapConstants.HOST, SoapConstants.HOST_MESSAGE);
		conn.setRequestProperty(SoapConstants.CONTENT_TYPE,
				SoapConstants.CONTENT_TYPE_MESSAGE);
		conn.setRequestProperty(SoapConstants.POST, SoapConstants.POST_MESSAGE);
		return conn;
	}

}